package org.edu.controller;

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.ListDataModel;

import org.edu.model.Address;
import org.edu.model.Student;
import org.edu.model.StudentLesson;
import org.edu.model.User;
import org.edu.service.StudentService;
import org.edu.service.UserService;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.SelectableDataModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller
@SessionScoped
public class StudentController extends ListDataModel<Student> implements Serializable, SelectableDataModel<Student> {

	private static final long serialVersionUID = 1L;
	@Autowired
	protected transient StudentService studentService;
	@Autowired
	private transient UserService userService;
	private Student student;
	private StudentLesson studentLesson;
	private User user;
	private Address addres;
	private List<User> userList;
	private List<Student> studentList;
	private List<Student> filterStudents;

	public  StudentController() {
		student = new Student();
		studentLesson = new StudentLesson();
		addres = new Address();
		
	}
	
	@PostConstruct
	public void init(){
		userList = userService.users();
		studentList = studentService.students();
		addres = new Address();
	}

	
	public String preparedAdding() {
		user = new User();
		student = new Student();
		addres = new Address();
		return "newstudent";
	}
	
	 public void onRowSelect(SelectEvent event) {  
	        FacesMessage msg = new FacesMessage("Student Selected", ((Student) event.getObject()).getUserStudent().getUser_number());;  
	        FacesContext.getCurrentInstance().addMessage(null, msg);  
	 }
	 
	@Override
	public Object getRowKey(Student object) {
		 return student.getUserStudent().getUser_number();
	}

	@Override
	public Student getRowData(String rowKey) {
		for(Student student : studentList){
			if(student.getUserStudent().getUser_number().equals(rowKey))  
                return student;  
		}
		return null;
	}
	
	public Student getStudent() {
		if(student == null){
			student = new Student();
		}
		return student;
	}

	public void setStudent(Student student) {
		this.student = student;
		
	}
	
	public User getUser() {
		if(user == null){
			user = new User();
		}
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Address getAddres() {
		if(addres == null){
			addres = new Address();
		}
		return addres;
	}

	public void setAddres(Address addres) {
		this.addres = addres;
	}

	public List<Student> getStudentList() {
		return studentList;
	}

	public void setStudentList(List<Student> studentList) {
		this.studentList = studentList;
	}

	public List<Student> getFilterStudents() {
		return filterStudents;
	}

	public void setFilterStudents(List<Student> filterStudents) {
		this.filterStudents = filterStudents;
	}

	public StudentLesson getStudentLesson() {
		if(studentLesson == null){
			studentLesson = new StudentLesson();
		}
		return studentLesson;
	}

	public void setStudentLesson(StudentLesson studentLesson) {
		this.studentLesson = studentLesson;
	}

	public List<User> getUserList() {
		return userList;
	}

}
